Lesson 3: Conditions and Loops

Conditions - Compare with "if", "elif" and "else"

  1. Evaluate expressions which produce TRUE or FALSE as outcome
  2. Based on the outcome of the expression a specific block of code is executed
  3. The expressions could be: equality (==) , inequality (!=), less/greater than (<)(>), less/greater than or equal
    (<=) (>=), membership (in, not in)
  4. Expressions can be combined using "and" and "or" and "and not"

Let us write a small code that mimics a dice by generating a random number between 1 and 6. It then asks you to enter a number between 1 and 6. If what you enter equals the dice value you win or else you loose.


In [1]:
#From the random library we import the randint function. 

#Generate a random number between 1 and 6

#!!!Input always passes strings !!!


Input a number between 1 and 6: 2
Sorry, the dice showed:  3

Loops - Repeat with "while" and Iterate with "for"

While loop

  1. A loop statement allows us to execute a statement or group of statements multiple times
  2. While loop is the simplest loop which executes a block of code until and expression is True
  3. A 'break' command can be used to exit any loop prematurely
  4. A 'continue' command can be used to skip ahead to the next iterations without exiting the loop

Now lets improve the previous function "dice()" such that the user has 5 attempts at entering the number and getting the right dice value.


In [3]:



Input a number between 1 and 6: 1
Sorry, the dice showed:  2
Input a number between 1 and 6: 1
Sorry, the dice showed:  6
Input a number between 1 and 6: 1
Sorry, the dice showed:  6
Input a number between 1 and 6: 1
Congratulations ! The value entered is the dice value
Input a number between 1 and 6: 1
Sorry, the dice showed:  5

Excercise : Can you edit the the previous code so that it stops asking the user to enter a number when the value entered matches the dice value ? Hint: You will need to use the "break" command within the while loop. Also remember that you can use the "return" command to pass values back to the calling function.


In [6]:
# Enter code here


Input a number between 1 and 6: 3
Sorry, the dice showed:  6
Input a number between 1 and 6: 3
Congratulations ! The value entered is the dice value

For loop

  1. For loops are used to iterate over data
  2. It makes it possible for you to traverse data structures without knowing how large they are
  3. You can even iterate over datastreams in real time

Let us write a program that asks the user to enter a text sentence and counts the number of times a particular character occurs in the sentence.


In [4]:



Input your sentence: My name in poo
Input the character that needs to be counted: o
Number of times the character ' o ' occurs is:  2

Excercise: Can you use the for loop so that it counts the number of times a given word occurs in a sentence ? Hint: Use the split() command to split the sentence into a list of words and then use the for loop to traverse through the list.


In [ ]: